<?php

namespace App\Admin\Controllers\Voter;

use App\Admin\Actions\Grid\Voter\SimulateVoteAction;
use App\Admin\Actions\Grid\Voter\VoterNoticeAction;
use App\Admin\Repositories\Voter\WxVoter;
use App\Models\Voter\WxVoterPlayer;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class WxVoterController extends AdminController
{
    public function voters(Request $request)
    {
        $q = $request->get('q');
        global $__MINI_GLOBAL_TENANT_ID__;
        if($__MINI_GLOBAL_TENANT_ID__ > 0){
            return \App\Models\Voter\WxVoter::where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->where('title', 'like', "%$q%")->where(function ($query) use ($q){
                $query->orWhere('id', (int)$q);
            })->paginate(null, ['id', 'title as text']);
        }
        return \App\Models\Voter\WxVoter::where('title', 'like', "%$q%")->orWhere('id', (int)$q)->paginate(null, ['id', 'title as text']);
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxVoter(), function (Grid $grid) {
            $grid->model()->orderBy('is_sticky', 'desc');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('owers', '头像')->display(function ($v) {
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower', '用户名称')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
//            $grid->column('master_user_avatar');
//            $grid->column('master_user_name');
//            $grid->column('template_id');
            $grid->column('title');
//            $grid->column('notice');
//            $grid->column('content');
            $grid->column('banners')->display(function ($v){
                $str = '';
                if($v){
                    foreach ($v as $img){
                        $str .= '<a href="'.$img.'" target="_blank"><img src="'.$img.'" style="width: 177px; height: 95px;"/></a>';
                        break;
                    }
                }
                return $str;
            });
            $grid->column('player_count', '选手总数')->display(function ($v){
                return '<a target="_blank" href="'.admin_url('voter/player?voter_id='.$this->id).'">'.(WxVoterPlayer::where('voter_id', $this->id)->count()).'</a>';
            });
//            $grid->column('is_open_enter');
//            $grid->column('is_exposure');
//            $grid->column('is_open_vote_comment');
//            $grid->column('is_open_player_comment');
//            $grid->column('is_can_be_anonymous');
//            $grid->column('is_hide_rank');
//            $grid->column('is_hide_vote_record');
//            $grid->column('is_hide_player_count');
//            $grid->column('music_url');
//            $grid->column('music_name');
//            $grid->column('is_music_auto');
//            $grid->column('start_time');
//            $grid->column('end_time');
//            $grid->column('enter_start_time');
//            $grid->column('enter_end_time');
//            $grid->column('user_day_limit');
//            $grid->column('user_total_limit');
//            $grid->column('player_called');
//            $grid->column('unit_called');
//            $grid->column('main_background_image');
//            $grid->column('user_background_image');
//            $grid->column('background_color');
//            $grid->column('text_color');
//            $grid->column('main_color');
//            $grid->column('button_bg_color');
//            $grid->column('button_text_color');
//            $grid->column('text_navigation_bar_color');
            $grid->column('is_sticky')->switch();
            $grid->column('status')->switch();
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->disableCreateButton();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new SimulateVoteAction())->setKey($this->id));
                $actions->append((new VoterNoticeAction())->setKey($this->id));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxVoter(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('master_user_avatar');
            $show->field('master_user_name');
            $show->field('template_id');
            $show->field('title');
            $show->field('notice');
            $show->field('content');
            $show->field('banners');
            $show->field('is_open_enter');
            $show->field('is_exposure');
            $show->field('is_open_vote_comment');
            $show->field('is_open_player_comment');
            $show->field('is_can_be_anonymous');
            $show->field('is_hide_rank');
            $show->field('is_hide_vote_record');
            $show->field('is_hide_player_count');
            $show->field('music_url');
            $show->field('music_name');
            $show->field('is_music_auto');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('enter_start_time');
            $show->field('enter_end_time');
            $show->field('user_day_limit');
            $show->field('user_total_limit');
            $show->field('player_called');
            $show->field('unit_called');
            $show->field('main_background_image');
            $show->field('user_background_image');
            $show->field('background_color');
            $show->field('text_color');
            $show->field('main_color');
            $show->field('button_bg_color');
            $show->field('button_text_color');
            $show->field('text_navigation_bar_color');
            $show->field('is_sticky');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxVoter(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->image('master_user_avatar')->url('files/uploads')->uniqueName()->autoUpload();
            $form->text('master_user_name');
            $form->text('template_id');
            $form->text('title');
            $form->textarea('notice');
            if(__system_is_model_enable('laradocs', 'dcat-neditor')){
                $form->neditor('content');
            }else{
                $form->editor('content');
            }
            $form->multipleImage('banners')->url('files/uploads')->uniqueName()->autoUpload();
            $form->switch('is_open_enter')->default(true);
            $form->switch('is_exposure')->default(true);
            $form->switch('is_open_vote_comment')->default(true);
            $form->switch('is_open_player_comment')->default(true);
            $form->switch('is_can_be_anonymous');
            $form->switch('is_hide_rank');
            $form->switch('is_hide_vote_record');
            $form->switch('is_hide_player_count');
            $form->text('music_url');
            $form->text('music_name');
            $form->switch('is_music_auto');
            $form->datetime('start_time');
            $form->datetime('end_time');
            $form->datetime('enter_start_time');
            $form->datetime('enter_end_time');
            $form->text('user_day_limit')->help('每天限制次数与总字数限制只生效一个，优先总字数限制');
            $form->text('user_total_limit');
            $form->text('player_called');
            $form->text('unit_called');
            $form->image('main_background_image')->url('files/uploads')->uniqueName()->autoUpload();
            $form->image('user_background_image')->url('files/uploads')->uniqueName()->autoUpload();
            $form->color('background_color');
            $form->color('text_color');
            $form->color('main_color');
            $form->color('button_bg_color');
            $form->color('button_text_color');
            $form->color('text_navigation_bar_color')->default('#000000');
            $form->switch('is_sticky')->default(0);
            $form->switch('status')->default(1);

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
